import React, { useState,useEffect } from 'react'
import './App.css'
import HelloComponent from './hello'
import NameEditComponent from './nameEdit'

function App() {
  // useState("默认值")
  const [name,setName] = useState("defaultUserName")
  const [editingName,setEditingName] = useState("defaultUserName")
  // const setUsernameState = 
  //   (e:React.ChangeEvent<HTMLInputElement>)=>{
  //     setName(e.target.value)
  // }
  const setUsernameState = ()=>{
    // 显示的值变为已经修改的值
      setName(editingName);
  }
  // const loadUserName=()=>{
  //   setTimeout(()=>{
  //     setName("name from async call")
  //     setEditingName("name from async call")
  //   },2500)
  // }
  useEffect(()=>{
    // loadUserName()
    setTimeout(()=>{
      setName("name from async call")
      setEditingName("name from async call")
      // 2.5s之后变为上述
    },2500)
  },[])
  return (
    <div className="App">
      <HelloComponent userName={name} age={18} />
      <NameEditComponent
        initialUserName={name}
        editingName={editingName}
        onNameUpdated={setUsernameState}
        onEditingNameUpdated={setEditingName}
        disabled={editingName == "" || editingName === name}
      />
    </div>
  )
}

export default App